import requests
import json

DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
API_KEY = "sk-58396912557140d5905f46327db61622"  # 替换为您的DeepSeek API密钥


def analyze_news(news_list):
    """使用DeepSeek API分析新闻列表"""
    if not news_list:
        return "没有可分析的新闻数据"

    # 构建提示词
    prompt = "请为以下新闻列表生成一个简短的摘要：\n"
    for i, news in enumerate(news_list):
        prompt += f"{i + 1}. {news['title']}\n"
    # 准备API请求
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {
        "model": "deepseek-chat",
        "messages": [
            {"role": "user", "content": prompt}
        ],
        "temperature": 0.7,
        "max_tokens": 200
    }

    try:
        response = requests.post(DEEPSEEK_API_URL, headers=headers, json=payload)
        response.raise_for_status()
        result = response.json()

        # 提取生成的摘要
        summary = result['choices'][0]['message']['content'].strip()
        return summary
    except Exception as e:
        return f"分析失败: {str(e)}"
